var express = require("express");
var router = express.Router();
const { BrandModel } = require("../model/model");

/* GET home page. */
router.get("/getBrand", async (req, res) => {
  const { pageNum, pageSize,search } = req.query;
  let query = {$and:[]};
  if(search){
    query.$and.push({name:{$regex:search}})
  }
  if(query.$and.length==0){
    query = {}
  }
  const list = await BrandModel.find(query)
    .skip((pageNum - 1) * pageSize)
    .limit(pageSize);

  const total = await BrandModel.find().count();
  const pageTotal = Math.ceil(total / pageSize);
  res.send({
    code: 200,
    data: {
      list,
      total,
      pageTotal,
      pageNum,
      pageSize,
    },
    msg: "获取成功",
  });
});


//进行单个品牌的修改
router.put("/putBrandId", async(req,res)=>{
  await BrandModel.updateOne({_id:req.body._id},req.body)
  res.send({
    code:200,
    msg:'修改成功'
  })
})


module.exports = router;
